#!/bin/sh
# -*- tcl -*-
# The next line is executed by /bin/sh, but not tcl \
exec tclsh "$0" ${1+"$@"}

# Author: jiyin@redhat.com
# This is tool to parse job-result.xml to test list
# tdom usage: http://wiki.tcl.tk/8984

lappend ::auto_path $::env(HOME)/lib /usr/local/lib /usr/lib64 /usr/lib
package require tdom
package require getOpt 3.0
namespace import ::getOpt::*

set prog [file tail $argv0]
array set Opt {}
array set InvalidOpt {}
set Args [list]
set ForwardOpt {}
set OptionList {
  "Options:" {
	{help h}   {arg n	help {Print this usage}}
	d          {arg n	help {debug mode} hide yes}
	logs       {arg n	help {get all logs}}
  }
}

# _parse_ argument
getOptions $OptionList $::argv Opt InvalidOpt Args ForwardOpt
if [info exist Opt(d)] {
	puts "\[$prog\]: Arguments:{$Args}"
	puts "\[$prog\]: ForwardOpt{$ForwardOpt}"
	parray InvalidOpt
	parray Opt
}
if {[array size InvalidOpt] > 0} {
	puts stderr "{Error} got invalide option\[s\]:"
	foreach {opt value} [array get InvalidOpt] {
		puts stderr "  $opt -> $value"
	}
	exit 1
}

proc Usage {} {
	puts "Usage0: $::prog \[options\] \[J:jobid\]"
	getUsage $::OptionList
}
if [info exist Opt(help)] {
	Usage
	exit 0
}

proc getXML {jobID} {
	set xml [exec bkr job-results --prettyxml $jobID]
	regsub {<\?xml .*?\?>} $xml {}
}

proc parseJobResult {jobid} {
	set XML [getXML $jobid]
	set doc [dom parse $XML]
	set root [$doc documentElement]

	set recipeSetList [$root selectNodes {descendant::recipeSet}]
	foreach recipeSet $recipeSetList {
		set recipeSetID [$recipeSet @id]
		set gSet {}
		set recipeList [$recipeSet selectNodes {descendant::recipe}]
		set hostN [llength $recipeList]
		set taskList [$recipeSet selectNodes {descendant::task}]
		foreach task $taskList {
			set tname [$task @name]
			if {$tname == "/kernel/kdump/setup-nfsdump"} {
				if {"kdump" ni $gSet} { lappend gSet kdump }
			}
		}

		foreach recipe $recipeList {
			set arch [$recipe @arch]
			set distro [$recipe @distro]
			set family [$recipe @family]
			set gset "$distro $arch"
			lappend gSet "$gset"
			set taskList [$recipe selectNodes {descendant::task}]

			foreach task $taskList {
				set tname [$task @name]
				if {$tname == "/distribution/install"} continue
				if {$tname == "/kernel/kdump/setup-nfsdump"} continue
				set tresult [$task @result]
				set params {}
				set paramList [$task selectNodes {descendant::param}]
				foreach param $paramList {
					set pname [$param @name]
					if {$pname == "DISTRO_BUILD"} continue
					if {$pname == "TestID"} continue
					append tname " $pname=[$param @value]"
				}

				#get subtest result
				set task_results [$task selectNodes results]
				set subresult [list]
				if {$task_results != ""} {
					foreach subres [$task_results childNodes] {
						set _res [$subres @result]
						set _path [$subres @path]
						lappend subresult "$_res $_path"
					}
					lappend tresult $subresult
				}

				lappend test($tname) $tresult
			}
		}
		puts stderr "recipeSet $jobid->RS:$recipeSetID $gSet"
		if [info exist ::Opt(logs)] {
			set recipelog [exec bkr job-logs RS:$recipeSetID]
			puts "$recipelog\n"
		} else {
			foreach {Name Res} [array get test] {
				set i 0
				foreach reciperes $Res {
					lassign $reciperes res subres
					puts "  $res {$Name}"
					foreach r $subres {
						puts "        $r"
					}
					puts ""
					incr i
				}
			}
		}
	}
}

#_main_
#Update test status and result
foreach jobID $Args {
	parseJobResult $jobID
}

